package cool.xinya.ess.security;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
    private final StandardPasswordEncoder encoder;

    public CustomCredentialsMatcher(StandardPasswordEncoder encoder) {
        this.encoder = encoder;
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        // 未加密密码
        String rawPassword = new String(((UsernamePasswordToken) token).getPassword());
        // 加密密码
        String encodedPassword = (String) info.getCredentials();
        return encoder.matches(rawPassword, encodedPassword);
    }
}
